<?php

class Painel_Form_Clientes extends Zend_Form
{

    public function init()
    {
        $front = Zend_Controller_Front::getInstance();
        $controller = $front->getRequest()->getControllerName();
        $module = $front->getRequest()->getModuleName();
        $url = $front->getBaseUrl().'/'.$module.'/'.$controller;
        
        $this->setName('form_edt');
        
        Zend_Loader::loadClass('dominio','library');
        
        $db = Zend_Db_Table_Abstract::getDefaultAdapter();
        
        $id = $front->getRequest()->getParam('id', 0);
        
        $data = $db->fetchRow('SELECT SQL_CACHE EST_ID,CID_ID FROM CLIENTES WHERE CLI_ID = ' . $id);
        
        $el = new Zend_Form_Element_Hidden('CLI_ID',array('disableLoadDefaultDecorators' =>true));
        $el->addDecorators(array(array('ViewHelper'),array('Errors')))
            ->addFilter('Int');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Checkbox('CLI_CPJSTA',array());
        $el->setLabel('Empresa');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Text('CLI_NOM');
        $el->setLabel('Nome');
        $el->setRequired(true);
        $el->setAttrib('class', 'form-control required');
        $el->setAttrib('required', '');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Text('CLI_DATNAS');
        $el->setLabel('Data Nascimento');
        $el->setAttrib('class', 'form-control data');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Radio('CLI_SEX');
        $el->setLabel('Sexo');
        $el->addMultiOptions(dominio::Sexo());
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Text('CLI_TEL');
        $el->setLabel('Telefone');
        $el->setAttrib('class', 'form-control telefone');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Text('CLI_CEL');
        $el->setLabel('Celular');
        $el->setAttrib('class', 'form-control telefone');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Text('CLI_RG');
        $el->setLabel('RG');
        $el->setAttrib('class', 'form-control numero');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Text('CLI_CPF');
        $el->setLabel('CPF');
        $el->setAttrib('class', 'form-control cpf');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Text('CLI_IE');
        $el->setLabel('I.E.');
        $el->setAttrib('class', 'form-control');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Text('CLI_CPJ');
        $el->setLabel('CNPJ');
        $el->setAttrib('class', 'form-control cnpj');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Text('CLI_CEP');
        $el->setLabel('CEP');
        $el->setAttrib('class', 'form-control cep');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Text('CLI_END');
        $el->setLabel('Endereço');
        $el->setAttrib('class', 'form-control res_endereco');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Text('CLI_BAI');
        $el->setLabel('Bairro');
        $el->setAttrib('class', 'form-control res_bairro');
        $this->addElement($el);
        
        $arr = array(''=>'Nenhum');
        $query = 'SELECT SQL_CACHE * FROM ESTADOS WHERE EST_STA = 1 ORDER BY EST_UF ASC';
        $result = $db->fetchAll($query);
        foreach($result as $r) $arr[$r['EST_ID']] = $r['EST_UF'].' - '.$r['EST_NOM'];
        
        $el = new Zend_Form_Element_Select('EST_ID',array('onchange' => ''));
        $el->setLabel('Estado')
        ->addMultiOptions($arr);
        $el->setRegisterInArrayValidator(false);
        $el->setAttrib('class', 'form-control required res_uf');
        $this->addElement($el);
        
        $arr = array(''=>'Nenhum');
        if($data['EST_ID'] > 0){
            $query = 'SELECT SQL_CACHE * FROM CIDADES WHERE EST_ID = '.$data['EST_ID'].' AND CID_STA = 1 ORDER BY CID_NOM ASC';
            $result = $db->fetchAll($query);
            foreach($result as $r) $arr[$r['CID_ID']] = $r['CID_NOM'];
        }
        $el = new Zend_Form_Element_Select('CID_ID',array('onchange' => ''));
        $el->setLabel('Cidade')
        ->addMultiOptions($arr);
        $el->setRegisterInArrayValidator(false);
        $el->setAttrib('class', 'form-control required res_cidade');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Text('CLI_EML');
        $el->setLabel('E-mail');
        $el->setAttrib('class', 'form-control email');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Textarea('CLI_OBS');
        $el->setLabel('Observação');
        $el->setAttrib('rows', '5');
        $el->setAttrib('class', 'form-control');
        $this->addElement($el);
        
        $img = glob('public/upload/'.$controller.'/'.$front->getRequest()->getParam('id').'.*');
        if(!isset($img[0])) $img = glob('public/media/images/sem_imagem.*');
        foreach($img as $src) {
            $el = new Zend_Form_Element('img',array( 'helper'=>'formNote', 'value'=>'<img src="'.$front->getBaseUrl().'/'.$src.'" />' ));
            $this->addElement($el);
        }
        $el = new Zend_Form_Element_File('CLI_FILE');
        $el->setLabel('Imagem')
	->addValidator('Extension', false, array('jpg', 'png', 'gif'));
        $el->setAttrib('class', 'form-control');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Checkbox('CLI_STA',array('checked'=>'checked'));
        $el->setLabel('Ativo');
        $this->addElement($el);
        
        $el = new Zend_Form_Element('br',array( 'helper'=>'formNote', 'value'=>'&nbsp;' ));
        $this->addElement($el);
        
        $btn = new Zend_Form_Element_Button('Salvar',array('disableLoadDefaultDecorators' =>true));
        $btn->setDecorators(array(array('Description', array('escape' => false, 'tag' => false))));
        $btn->setDescription('<button type="submit" class="btn btn-lg btn-success"><span class="glyphicon glyphicon-floppy-saved" aria-hidden="true"></span> Salvar</button>');
        $this->addElement($btn);
        
        $btn = new Zend_Form_Element_Button('Cancelar',array('disableLoadDefaultDecorators' =>true));
        $btn->setDecorators(array(array('Description', array('escape' => false, 'tag' => false))));
        $btn->setDescription('<a href="'.$url.'" class="btn btn-lg btn-danger"><span class="glyphicon glyphicon-floppy-remove" aria-hidden="true"></span> Cancelar</a>');
        $this->addElement($btn);
     
    }


}

